﻿$.Controller.extend('Preferences',
/* @Static */
{

},

/* @Prototype */
{

init: function () {
    var me = this;
    this.tooltipColor = Settings.get_tooltipColor();
    this.colorPicker = $('.color_picker').color_picker(this.tooltipColor).controller();
    this.colorPicker.addEventListener("select", function (color) {
        me.tooltipColor = color;
        me.colorPicker.hide();
        $('.current_color_container').css('background-color', color);
        me.tooltipController.setBackgroundColor(color);
    });
    $('.languages').val(Settings.get_language());
    $('.key_codes').val(Settings.get_keyCode());
    $('.font_size').val(Settings.get_fontSize());
    $('.current_color_container').css('background-color', Settings.get_tooltipColor());
    $('.color_container').click(function () { event.stopImmediatePropagation(); me.colorPicker.toggle(); });


    $(document.body).click(function () { me.colorPicker.hide(); });
},

ready: function () {

    var createRequestUrl = function (text, sourceLanguage) {
        //Check if google dictionary supports the detected language
        var sourceLanguageObj = GoogleDictionary.sourceLanguages.get(sourceLanguage);

        var targetLanguage = $('.languages').val();

        if (sourceLanguageObj != null) {
            //check if google dicationary translates from the detected language to the user's prefered language 
            if (sourceLanguageObj.targetLanguages.contains(targetLanguage)) {
                //generate the google-dictionary request URL
                return GoogleDictionary.generateRequest(text, sourceLanguage, targetLanguage);

            }
            else { //Google dictionary does not support the language pair. We'll translate it by default to english.
                return GoogleDictionary.generateRequest(text, sourceLanguage, 'en');
            }
        }
        else { //Google dictionary does not support at all the detected language
            return GoogleTranslate.generateRequest(text, sourceLanguage, targetLanguage);
        }
    }


    var me = this;
    var rootPath = chrome.extension.getURL("/");
    var viewUrl = chrome.extension.getURL('views/vocabbi_tooltip/tooltip');
    var params = { text: null, background_color: Settings.get_tooltipColor(), root_path: rootPath }
    this.tooltip = $($.View(viewUrl, params)).hide();
    $(document.body).append(this.tooltip);
    this.tooltipController = this.tooltip.vocabbi_tooltip().controller();
    this.tooltipController.addEventListener('textSelect', function (text, language) {
        var requestUrl = createRequestUrl(text, language);
        //open the google dictionary in a new tab.
        window.open(requestUrl);
    });

    window.setTimeout(function () { me.setTooltipContent(Settings.get_language()); }, 100);


    $('.languages').change(function () {
        me.setTooltipContent();
    });

    $('.font_size').change(function () {
        me.setTooltipContent();
    });

},

setTooltipContent: function () {
    var me = this;
    var rect = $('.sample_text')[0].getBoundingClientRect();
    console.log('rect- ' + rect.left + ' ' + rect.top);
    var text = $('.sample_text').text();
    var fontSize = $('.font_size').val();
    var language = $('.languages').val();

    google.language.translate(text, 'en', language,
                    function (result) {
                        me.tooltipController.showTooltip(rect);
                        me.tooltipController.setContent(text, 'en', result.translation, language, fontSize);
                        me.tooltipController.setPosition();
                    }
                );
},

".save click": function (el, ev) {
    var language = $('.languages option:selected').val();
    Settings.set_language(language);


    Settings.set_tooltipColor(this.tooltipColor);


    var keyCode = $('.key_codes option:selected').val();
    Settings.set_keyCode(keyCode);

    var fontSize = $('.font_size option:selected').val();
    Settings.set_fontSize(fontSize);

    $('.save_message').css('left', ($(document).width() / 2 - $('.save_message').outerWidth() / 2) + 'px');
    $('.save_message').show(0, function () { window.setTimeout("$('.save_message').hide()", 5000); });

},

".cancel click": function () {
    chrome.tabs.getSelected(null, function (tab) { chrome.tabs.remove(tab.id); });
}

});


